package cc.ufinity.networkdiagnose.widget;

import cc.ufinity.networkdiagnose.util.StringUtils;
import cc.ufinity.networkdiagnose.view.R;
import android.app.Activity;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MyMobileCfgWidget extends GeneralTestWidget implements View.OnClickListener {

    public final static String PREFS_NAME = "mymobile";
    public final static String PREFS_KEY = "number";

    private Activity ctx;
    private Button btnSave;
    private Button btnBack;
    private EditText mobile;

    public MyMobileCfgWidget(Activity context) {
        super(context);
        this.ctx = context;
        inflate(context, R.layout.mymobile, this);
        btnSave = (Button) this.findViewById(R.id.btn_mobilecfg_save);
        btnBack = (Button) this.findViewById(R.id.btn_mobilecfg_back);
        mobile = (EditText) this.findViewById(R.id.mobilecfgeditText);
        btnSave.setOnClickListener(this);
        btnBack.setOnClickListener(this);
        SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, 0);
        String tel = settings.getString(PREFS_KEY, "");
        mobile.setText(tel);
    }

    private void save() {
        String input = mobile.getText().toString();
        if (StringUtils.isEmpty(input)) {
            showMessage(ctx, "请输入本机号码.");
        } else {
            SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, 0);
            SharedPreferences.Editor editor = settings.edit();
            editor.putString(PREFS_KEY, input);
            editor.commit();
            showMessage(ctx, "本机号码已保存.");
            this.backToConfig(ctx);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn_mobilecfg_save:
            save();
            break;
        case R.id.btn_mobilecfg_back:
            this.backToConfig(ctx);
            break;
        }
    }

    
}
